<template>
  <div id="userLoginView">
    <h2 style="margin-bottom: 16px">用户登录</h2>
    <a-form
      style="max-width: 480px; margin: 0 auto"
      label-align="left"
      auto-label-width
      :model="form"
      @submit="handleSubmit"
    >
      <a-form-item
        :rules="[
          { required: true, message: '账号不能为空' },
          { minLength: 3, message: '账号长度不能低于三位' },
        ]"
        field="userAccount"
        label="账号">
        <a-input v-model="form.userAccount" placeholder="请输入账号" />
      </a-form-item>
      <a-form-item
        :rules="[
          { required: true, message: '密码不能为空' },
          { minLength: 8, message: '密码长度不能低于八位' },
        ]"
        field="userPassword"
        tooltip="密码不少于 8 位"
        label="密码">
        <a-input-password
          v-model="form.userPassword"
          placeholder="请输入密码"
        />
      </a-form-item>
      <a-form-item justify-content="center">
        <a-button type="primary" html-type="submit" style="width: 120px ;margin: 0 auto">
          登录
        </a-button>
      </a-form-item>
    </a-form>
  </div>
</template>
<script setup lang="ts">
import { reactive } from "vue";
import { useRouter } from "vue-router";
import { UserControllerService, UserLoginRequest } from "../../../generated";
import { useStore } from "vuex";
import message from "@arco-design/web-vue/es/message";
import { Message } from "@arco-design/web-vue";

const router = useRouter();
const store = useStore();

const form = reactive({
  userAccount: "",
  userPassword: ""
} as UserLoginRequest);
const handleSubmit = async () => {
  if (form.userAccount.length < 3 || form.userPassword.length < 6) {
    Message.error("账号或密码长度不符合要求");
    return;
  }
  const res = await UserControllerService.userLoginUsingPost(form);
  if (res.code === 0) {
    store.dispatch("user/getLoginUser");
    router.push({ path: "/", replace: true });
  } else {
    message.error(res.message);
  }
};


</script>

<style scoped>

</style>
